package com.yc.mypredicatefactory;

import org.springframework.cloud.gateway.handler.predicate.AbstractRoutePredicateFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;

import java.time.LocalDate;
import java.util.List;
import java.util.function.Predicate;

@Component
public class HolidayRoutePredicateFactory extends AbstractRoutePredicateFactory<HolidayRoutePredicateFactory.Config> {

    public HolidayRoutePredicateFactory() {
        super(Config.class);
    }

    @Override
    public Predicate<ServerWebExchange> apply(Config config) {
        return exchange -> {
            LocalDate today = LocalDate.now();
            // 你可以根据实际情况替换节假日判断逻辑，比如从配置中心获取或调用API
            List<LocalDate> holidays = List.of(
                    LocalDate.of(2025, 1, 1),
                    LocalDate.of(2025, 5, 1),
                    LocalDate.of(2025, 10, 1)
            );
            boolean isHoliday = holidays.contains(today);

            // 如果节假日，则让路由命中，否则不命中
            return config.isHoliday() == isHoliday;
        };
    }

    public static class Config {
        private boolean holiday;

        public boolean isHoliday() {
            return holiday;
        }

        public void setHoliday(boolean holiday) {
            this.holiday = holiday;
        }
    }
}
